using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.AI;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class MouseManager : singleTon<MouseManager>
{
    public event Action<Vector3> OnMouseClicked;
    public event Action<GameObject> OnEnemyAttacked;
    public Texture2D point, doorway, attack, target, arrow, skill;
    RaycastHit hitInfo;
    public float speed;
    private Vector3 Target;
    private bool isUsingSkill;
    protected override void Awake()
    {
        base.Awake();
        DontDestroyOnLoad(this);
    }
    private void Update()
    {
        SetCurSorTexture();
        if (InteractWithUI()) return;
        //MouseController();

    }


    public void setIsUsingSkill(bool value)
    {
        isUsingSkill = value;
    }
    void SetCurSorTexture()
    {
        if (SceneManager.GetActiveScene().name != "Main")
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hitInfo) && !isUsingSkill)
            {
                switch (hitInfo.collider.gameObject.tag)
                {
                    case "Item":
                        Cursor.SetCursor(point, new Vector2(0, 0), CursorMode.Auto);
                        break;
                    case "Ground":
                        Cursor.SetCursor(target, new Vector2(0, 0), CursorMode.Auto);
                        break;
                    case "Enemy":
                        Cursor.SetCursor(attack, new Vector2(0, 0), CursorMode.Auto);
                        break;
                    case "Portal":
                        Cursor.SetCursor(doorway, new Vector2(0, 0), CursorMode.Auto);
                        break;
                    default:
                        Cursor.SetCursor(target, new Vector2(0, 0), CursorMode.Auto);
                        break;
                }
            }
        }
    }
    
    void MouseController()
    {
        if (Input.GetMouseButtonDown(0) && hitInfo.collider != null)
        {
            if (hitInfo.collider.gameObject.CompareTag("Ground")) 
            {
                OnMouseClicked?.Invoke(hitInfo.point);
            }
            if (hitInfo.collider.gameObject.CompareTag("Item"))
            {
                OnMouseClicked?.Invoke(hitInfo.point);
            }
            if (hitInfo.collider.gameObject.CompareTag("Portal"))
            {
                OnMouseClicked?.Invoke(hitInfo.point);
            }
            if (hitInfo.collider.gameObject.CompareTag("Enemy"))
            {
                OnEnemyAttacked?.Invoke(hitInfo.collider.gameObject);
            }
            if (hitInfo.collider.gameObject.CompareTag("Attackable"))
            {
                OnEnemyAttacked?.Invoke(hitInfo.collider.gameObject);
            }
        }
    }


    bool InteractWithUI()
    {
        if (GameManager.Instance.playerStats != null)
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        return false;
    }
}
